package com.jiuzhi.logger.util;/**
 * Created with IDEA
 * author:tangsixiang@163.com
 * Date:2018/11/30
 * Time:11:19
 */

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jiuzhi.logger.pojo.NginxDemain;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.util.Collector;

/****
 @ClassName NginxFlatMapper
 @Description TODO
 @Author tangsixiang
 @create: 2018-11-30 11:19
 **/

public class NginxFlatMapper  implements FlatMapFunction<String, NginxDemain> {

    @Override
    public void flatMap(String value, Collector<NginxDemain> out) throws Exception {

             try{
                 //把数据转换为对象
                 NginxDemain nginxDemain=new ObjectMapper().readValue(value,NginxDemain.class);
                    //请求字符串二次处理
                    nginxDemain.setRequest(changeReqeustValue(nginxDemain.getRequest()));

                    out.collect(nginxDemain);
             }catch (Exception e){
                e.printStackTrace();
             }


    }


    public String  changeReqeustValue(String request) {

        try {
            //字符串业务解析操作  截取中间的字符串 去掉 有？参数的模块
            String[] arry = request.split(" ");
            String newReq = arry[2];
            if(newReq.indexOf("?")>=1){
                newReq=newReq.split("\\?")[0];
            }
            return newReq;

        } catch (Exception e) {


        }

        return request;

    }



}
